package com.xiang.api.entity;

import com.alibaba.fastjson2.JSONObject;
import com.xiang.api.Util;

import lombok.Data;

@Data
public class Message {
  public String sid;
  public long createTime = Util.time();
  public String type;
  public Object data;
  public String message;
  public String md5;

  public Message() {
  }

  public Message(String message) {
    this.message = message;
  }

  public Message(String message, String type) {
    this.message = message;
    this.type = type;
  }

  public Message(String message, String type, Object data) {
    this.message = message;
    this.type = type;
    this.data = data;
  }

  public Message(String message, boolean typeError) {
    this.message = message;
    if (typeError) {
      this.type = "error";
    }
  }

  public Message(String type, JSONObject data) {
    this.type = type;
    this.data = data;
  }

  public Message(Message msg) {
    sid = msg.sid;
    type = msg.type;
    data = msg.data;
  }

  public Message(Object data) {
    this.data = data;
  }

  public Message(Object data, String type) {
    this.data = data;
    this.type = type;
  }

  public static Message success(String message, Object data) {
    Message ret = new Message(data);
    ret.type = "success";
    ret.message = message;
    return ret;
  }

  public static Message init(String type, JSONObject data) {
    return new Message(type, data);
  }

  public static Message err(String message) {
    return new Message(message, true);
  }
}
